package com.zcn.io.web.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zcn.io.common.exception.ServiceException;
import com.zcn.io.web.api.query.AnswerStatisticQuery;
import com.zcn.io.web.api.entity.AnswerRecord;
import com.zcn.io.web.api.entity.OrderTransferPhoneRecord;
import com.zcn.io.web.api.vo.AnswerRecordVo;
import com.zcn.io.web.api.vo.OrderMainVo;
import com.zcn.io.web.mapper.AnswerRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/4 15:35
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class AnswerRecordService {

    private final OrderService orderService;

    private final AnswerRecordMapper answerRecordMapper;

    private final OrderTransferPhoneRecordService orderTransferPhoneRecordService;

    public List<AnswerRecord> getByOrderId(String orderId, String startTime, String endTime) {
        LambdaQueryWrapper<AnswerRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnswerRecord::getOrderId, orderId);
        if (StrUtil.isNotEmpty(startTime)) {
            wrapper.ge(AnswerRecord::getAnswerDate, startTime);
        }
        if (StrUtil.isNotEmpty(endTime)) {
            wrapper.le(AnswerRecord::getAnswerDate, endTime);
        }
        wrapper.orderByDesc(AnswerRecord::getAnswerDate);
        return answerRecordMapper.selectList(wrapper);
    }

    public AnswerRecordVo getDetailByOrderId(AnswerStatisticQuery query) {
        //获取订单信息
        OrderMainVo orderMainVo = orderService.getById(query.getOrderId());
        //获取订单手机号变更记录
        List<OrderTransferPhoneRecord> phoneChangeRecord = orderTransferPhoneRecordService.getAllByOrderId(query.getOrderId());
        //获取电话接听记录
        List<AnswerRecord> answerRecordList = this.getByOrderId(query.getOrderId(), query.getStartTime(), query.getEndTime());
        AnswerRecordVo answerRecordVo = new AnswerRecordVo();
        answerRecordVo.setOrderMain(orderMainVo);
        answerRecordVo.setAnswerRecordList(answerRecordList);
        answerRecordVo.setPhoneChangeRecords(phoneChangeRecord);
        return answerRecordVo;
    }

    public void updateAll(List<AnswerRecord> answerRecords) {
        if (CollectionUtil.isEmpty(answerRecords)) {
            return;
        }
        //判断日期是否重复
        long count = answerRecords.stream().map(AnswerRecord::getAnswerDate).distinct().count();
        if (count != answerRecords.size()) {
            throw new ServiceException(600, "接听日期不能重复");
        }
        Map<Boolean, List<AnswerRecord>> saveMap = answerRecords.stream().collect(Collectors.partitioningBy(item -> StrUtil.isEmpty(item.getId())));
        List<AnswerRecord> insertList = saveMap.get(Boolean.TRUE);
        if (CollectionUtil.isNotEmpty(insertList)) {
            for (AnswerRecord answerRecord : insertList) {
                answerRecordMapper.insert(answerRecord);
            }
        }
        List<AnswerRecord> updateList = saveMap.get(Boolean.FALSE);
        if (CollectionUtil.isNotEmpty(updateList)) {
            for (AnswerRecord answerRecord : updateList) {
                answerRecordMapper.updateById(answerRecord);
            }
        }
    }

    public void deleteById(String id) {
        answerRecordMapper.deleteById(id);
    }
}
